package cn.itcast.controller;


import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController{
    @Autowired
    private UserService userService;
@Autowired
protected HttpServletRequest request;
@Autowired
protected HttpSession session;
@Autowired
private ModuleService moduleService;
    @RequestMapping("/login")
    public String login(String email,String password) {

        //    1、 做非空判断
        if(StringUtils.isEmpty(email)||StringUtils.isEmpty(password)){
            request.setAttribute("error","邮箱或密码不能为空");
            return "forward:login.jsp";
        }
        //        使用shiro的认证方式：1、创建令牌  2、获取主题  3、开始认证
       password= new Md5Hash(password,email,2).toString();
        UsernamePasswordToken token = new UsernamePasswordToken(email, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
        } catch (AuthenticationException e) {
//            e.printStackTrace();
            request.setAttribute("error","邮箱或者密码有误");
        }
        User user = (User) subject.getPrincipal();
        session.setAttribute("loginUser",user);
        List<Module> moduleList = moduleService.findModuleListByUser(user);
        session.setAttribute("modules",moduleList);
        return "home/main";
        /*//2、根据邮箱查询用户
        User user = userService.findByEmail(email);
        password = new Md5Hash(password,email,2).toString();//明文加密，以方便做比较

        //3、比较密码是否匹配
        if(user ==null || !user.getPassword().equals(password)) {
            request.setAttribute("error","用户名或密码错误");
            return "forward:login.jsp";
        }else{
            //将用户信息保存到session中
            session.setAttribute("loginUser",user);
            //根据登录用户查询模块信息
            List<Module> moduleList = moduleService.findModuleListByUser(user);
            session.setAttribute("modules",moduleList);
            return "home/main";
        }*/
    }

    //退出
    @RequestMapping(value = "/logout",name="用户登出")
    public String logout(){
        SecurityUtils.getSubject().logout();   //登出
        return "forward:login.jsp";
    }

    @RequestMapping("/home")
    public String home(){
	    return "home/home";
    }

    public static void main(String[] args) {
        System.out.println(new Md5Hash("123456","admin@export.com",2));
    }
}
